﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Client.Resources;

namespace Client
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        ClientController controller = new ClientController();
        public MainPage()
        {
            InitializeComponent();



            controller.ServerFound += controller_ServerFound;
            controller.StartFindServer("111");
            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        void controller_ServerFound(object sender, ServerFoundArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                foreach (ServerInformation info in e.ServerNames)
                {
                    ServerItem item = new ServerItem();
                    item.SetContent(info);
                    item.Tap += item_Tap;
                    servers.Children.Add(item);
                }
            });
        }

        void item_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            ServerItem item = sender as ServerItem;
            if (item != null)
            {
                controller.FileTransferCompleted += controller_FileTransferCompleted;
                controller.ConnectToServer(item.Content);
            }
        }

        void controller_FileTransferCompleted(object sender, object e)
        {
            Dispatcher.BeginInvoke(() =>
           {
               AppTitle.Text = "File Completed";
           });
        }

        // Sample code for building a localized ApplicationBar
        //private void BuildLocalizedApplicationBar()
        //{
        //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
        //    ApplicationBar = new ApplicationBar();

        //    // Create a new button and set the text value to the localized string from AppResources.
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // Create a new menu item with the localized string from AppResources.
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
    }
}